package com.szewec.ps.ui.common;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;

import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.szewec.ps.R;
import com.szewec.ps.base.BaseActivity;
import com.szewec.ps.base.Constants;
import com.szewec.ps.base.ListBaseAdapter;
import com.szewec.ps.base.SuperViewHolder;
import com.szewec.ps.databinding.ActivityMultiSelectPageBinding;
import com.szewec.ps.model.CheckBoxValue;
import com.szewec.ps.model.PresentValue;
import com.szewec.ps.model.ViewModeValue;
import com.szewec.ps.myview.FlowLayoutManager;
import com.szewec.ps.util.CommonUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MultiSelectPage extends BaseActivity {
    private ActivityMultiSelectPageBinding binding;

    /********字典start***********/
    private ArrayList<String> typeDict = new ArrayList<>();
    /********字典end***********/

    private MultiSelectAdapter multiSelectAdapter;
    private List<CheckBoxValue> checkBoxValueList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_multi_select_page);
        //初始化数据
        initData();
        //初始化UI
        refreshUI();
    }

    private void initData(){
        //获取intent中的数据
        PresentValue<String> presentValue = (PresentValue<String>) getIntent().getSerializableExtra(Constants.EXTRA_KEY.MULTI_SELECT_VALUE);
        String value = presentValue.getT();
//        if(Constants.EXTRA_KEY.MULTI_SELECT_PIPING.equals(presentValue.getFlag())){
//            //共用一个界面， 通过自定义的标志初始化字典
//            typeDict = new ArrayList<>(Arrays.asList(getResources().getStringArray(R.array.pipingType)));
//        }
        //需要初始化选项字典
        for(String type: typeDict){
            CheckBoxValue checkBoxValue = new CheckBoxValue(false, type);
            checkBoxValueList.add(checkBoxValue);
        }
        if(!TextUtils.isEmpty(value)){
            String[] stringArray = value.split(Constants.COMMA);
            if(stringArray != null){
                List<String> list =  Arrays.asList(stringArray);
                for(String v: list){
                    for(CheckBoxValue checkBoxValue: checkBoxValueList){
                        if(checkBoxValue.getValue().equals(v)){
                            checkBoxValue.setIschecked(true);
                        }
                    }
                }
            }
        }
    }

    /**
     * 初始化UI
     */
    private void refreshUI() {
        //初始化附件适配器
        initRecycleView();
        //初始化界面数据
        initView();
    }

    /**
     * 初始化界面样式
     */
    private void initView() {
        ViewModeValue viewModeValue = new ViewModeValue(this, Constants.PRESENT_VALUE_FLAG.ADD, binding.titleBar, binding.llContainer);
        CommonUtils.setViewMode(viewModeValue, this::finish, this::doSave);
    }

    /**
     * 初始化适配器
     */
    private void initRecycleView(){
        //初始化管道适配器
        multiSelectAdapter = new MultiSelectAdapter(this);
        multiSelectAdapter.addAll(checkBoxValueList);
        binding.recyclerView.setAdapter(new LRecyclerViewAdapter(multiSelectAdapter));
        binding.recyclerView.setLayoutManager(new FlowLayoutManager(this, false));
    }


    /**
     * 自定义构筑物或设备调查调查适配器
     */
    private class MultiSelectAdapter extends ListBaseAdapter<CheckBoxValue> {


        public MultiSelectAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_multi_select;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            CheckBoxValue checkBoxValue = mDataList.get(position);
            TextView textView = holder.getView(R.id.tv_value);
            textView.setText(checkBoxValue.getValue());
            //根据状态设置tag显示
            if (checkBoxValue.getIschecked()) {
                textView.setBackgroundResource(R.drawable.item_checked);
                textView.setTextColor(getResources().getColor(R.color.C2A84FC));
            } else {
                textView.setBackgroundResource(R.drawable.item_unchecked);
                textView.setTextColor(getResources().getColor(R.color.C666666));
            }
            //行点击监听事件
            holder.itemView.setOnClickListener(view -> {
                checkBoxValue.setIschecked(!checkBoxValue.getIschecked());
                multiSelectAdapter.notifyDataSetChanged();
            });
        }
    }

    /**
     * 保存用户选择的多选内容
     */
    private void doSave(){
        List<String> list = new ArrayList<>();
        for(CheckBoxValue checkBoxValue : checkBoxValueList){
            if(checkBoxValue.getIschecked()){
                list.add(checkBoxValue.getValue());
            }
        }
        Intent intent = new Intent();
        intent.putExtra(Constants.EXTRA_KEY.MULTI_SELECT_VALUE, TextUtils.join(Constants.COMMA, list));
        setResult(Constants.RESULT_CODE.USELESS, intent);
        finish();
    }
}
